﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;

namespace GetIMEIWeb
{
    public class FileHelper
    {
        public static void CheckDirectory(string path)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }

        public String readUrl()
        {
            String path = AppDomain.CurrentDomain.BaseDirectory + "Data\\url.txt";
            String pathD = AppDomain.CurrentDomain.BaseDirectory + "Data";
            CheckDirectory(pathD);
            if (File.Exists(path))
            {
                StreamReader srReadFile = new StreamReader(path);
                String strReadLine = "";
                while (!srReadFile.EndOfStream)
                {
                    strReadLine = srReadFile.ReadLine();
                }
                // 关闭读取流文件
                srReadFile.Close();
                return strReadLine;
            }
            else
            {
               StreamWriter sw=   File.CreateText(path);
               sw.Write("");
               sw.Close();
                return "";
            }
        }

        public bool writeUrl(string url)
        {
            String path = AppDomain.CurrentDomain.BaseDirectory + "Data\\url.txt";
            try
            {
                StreamWriter sw = new StreamWriter(path, false);
                sw.Write(url);
                sw.Close();
                return true;
            }
            catch (Exception _Exception)
            {
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }
            return false;
        }
         
        public static bool SaveLog(string _FileName, string contents)
        {
            try
            {
                StreamWriter sw = new StreamWriter(_FileName, true); 
                sw.WriteLine(contents); 
                sw.Close(); 
                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }

            // error occured, return false
            return false;
        }

        public static void SaveToFile(string filePath, string contents)
        {
            byte[] bytes = Convert.FromBase64String(contents);
            ByteArrayToFile(filePath, bytes);
        }
        public static bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
        {
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                // Writes a block of bytes to this stream using data from a byte array.
                _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

                // close file stream
                _FileStream.Close();

                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }

            // error occured, return false
            return false;
        }
    }
}